#include "FadeTo.h"
#include "T_Node.h"
#include "T_Scene.h"

FadeTo *FadeTo::create(short alpha,float duration){
	FadeTo *ret=new FadeTo();
	ret->m_AlphaDesti=alpha;
	ret->setDuration(duration);
	return ret;
}

bool FadeTo::init(T_Node *node){
	if(ActionInterval::init(node)){
		m_AlphaOri=node->getAlpha();
		m_DeltaAlpha=(m_AlphaDesti-m_AlphaOri);
		return true;
	}
	return false;
}

void FadeTo::startWithTarget(T_Node *node){
	if(init(node)){
		T_Scene *scene=node->getScene();
		if(NULL==scene)return;
		scene->getActionPool().removeAll(node,getActionType());
		scene->getActionPool().add(this);
	}
}

void FadeTo::update(int dt){
	ActionInterval::update(dt);
	short alpha=(short)(m_AlphaOri+m_DeltaAlpha*(m_TimeElapsed/m_Duration));
	m_Target->setAlpha(alpha);
}